package com.hzdesk.media.koreabatchmatvector.request;

import android.content.Context;
import android.os.ParcelUuid;
import android.util.Log;


import com.hzdesk.media.koreabatchmatvector.inter.ICallBack;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2018/5/25.
 */

public class UploadFileCallBackRequest {
    private static final String TAG = UploadFileCallBackRequest.class.getSimpleName();
    private Model model = Model.getInstance();
    private Context context;
    private String Url;

    public UploadFileCallBackRequest(Context context, String url) {
        this.context = context;
        Url = url;
    }

    public interface OnUploadFileCallBackListener{
        void success();

        void fail(String s);
    }
    public OnUploadFileCallBackListener onUploadFileCallBackListener;
    private String Success = "SUCCESS";
    private String Fail = "FAIL";

    public void setOnUploadFileCallBackListener(OnUploadFileCallBackListener onUploadFileCallBackListener) {
        this.onUploadFileCallBackListener = onUploadFileCallBackListener;
    }
    public void setRequestBody(String json) {
        Log.d(TAG, json);
        getRequest(Url, json);
    }
    public void getRequest(String url,String json) {
        model.postSynchronized(context, url, json, new ICallBack() {
            @Override
            public void result(String s) {
                Log.d(TAG,s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.has("message")) {
                        if (jsonObject.getString("message").equals(Success)) {
                            onUploadFileCallBackListener.success();
                        }
                        if (jsonObject.getString("message").equals(Fail)) {
                            onUploadFileCallBackListener.fail(s);
                        }
                    }
                } catch (JSONException e) {
                    //e.printStackTrace();
                    Log.d(TAG,s);
                }

            }
        });
    }
}
